/*
 * ServerSocketForClient.h
 *
 *  Created on: 2013-3-14
 *      Author: root
 */

#ifndef SVRSOCKET4USR_H_
#define SVRSOCKET4USR_H_
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <net/if.h>
#include <netinet/tcp.h>

#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <vector>

#include "debug.h"
#include "iostream"
#include "types.h"
using namespace std;

#define USRPORT 20132    /*服务器接收数据端口号 */

class SvrSocket4Usr {
public:
	 //打开服务端口,等待连接
	SvrSocket4Usr();
	~SvrSocket4Usr(){};
	int  StartSvr();   //启动命令端口服务
	void StopSvr();    //停止命令端口服务

	void CloseFd(int fd);

	void ParseData(int fd);
	bool CheckCmd(Cmd_t &cmd);

	int RecCmd(int fd,Cmd_t &cmd);
	int RecCmd(int fd,DevInfo_t &devInfo);
	int RecCmd(int fd,u32Value_t &value);

	bool IsCanBind(int devId,int fd);
	vector<int> m_vctFd;
	int GetVctFd(int devId);

	int  GetDevId(int fd);

	int SendCmd(int fd,Hu32Value_t &send);
	int AnswerErr(int fd,uint16 cmd,int err);


	void BindDevId(int fd,int devId);
	void UnBindDevId(int fd);

	void DisMap(int len);

	int m_devId[1024];

	int  m_ListenSock;  //侦听端口
	int  m_ClientSock;  //客户端口

	char m_sClientIP[20];  //用户软件IP
	fd_set m_readfds;


private:
	pthread_t m_pthServer;

};

#endif /* USRSVRSOCKET_H_ */
